#!/bin/sh /etc/rc.common
START=50

config_t_get() {
	local index=0
	[ -n "$4" ] && index=$4
	local ret=$(uci -q get autoreboot.@$1[$index].$2 2>/dev/null)
	echo ${ret:=$3}
}

run_reboot()
{
	local enable=$(config_t_get login enable 0)
	if [ "$enable" = 1 ]; then
		local minute=$(config_t_get login minute)
		local hour=$(config_t_get login hour)
		local week=$(config_t_get login week)
    if [ "$minute" = 0 ] ; then
        minute="00"
		fi
    if [ "$week" = 7 ] ; then
        week="*"
		fi
		sed -i '/reboot/d' /etc/crontabs/root >/dev/null 2>&1
		/etc/init.d/cron restart
		echo "$minute $hour * * $week sleep 5 && touch /etc/banner && reboot" >> /etc/crontabs/root
		echo "Auto REBOOT has started."
	else
		sed -i '/reboot/d' /etc/crontabs/root >/dev/null 2>&1
		/etc/init.d/cron restart
		echo "Auto REBOOT has started."
	fi
}

start()
{
	run_reboot
}

stop()
{
	sed -i '/reboot/d' /etc/crontabs/root >/dev/null 2>&1
	/etc/init.d/cron restart
	echo "Auto REBOOT has stoped."
}
